package com.example.webflux.controller;

import com.example.webflux.bean.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Copyright(C), 2020-2021, AmbroseCdMeng
 * FileName:    UserController
 * Author:      AmbroseCdMeng
 * Date:        2021/1/1519:05
 * Description: 用户相关控制器。实现 WebFlux 操作数据功能
 * History:
 * <author>     <time>      <version>       <desc>
 * 作者姓名     修改时间        版本号           描述
 */
@RestController
@RequestMapping("/user")
public class UserController {
    Map<Long, User> users = new HashMap<>();

    /**
     * 初始化构造数据
     *
     * @throws Exception
     */
    // 依赖关系注入完成之后，执行初始化
    @PostConstruct
    public void init() throws Exception {
        users.put(Long.valueOf(1), new User(1, "AmbroseCdMeng", 22));
        users.put(Long.valueOf(1), new User(2, "XiaoYuEr", 21));
    }

    /**
     * 获取所有用户
     * @return
     */
    @GetMapping("/list")
    public Flux<User> getAll() {
        return Flux.fromIterable(users.entrySet().stream()
                        .map(entry -> entry.getValue()).collect(Collectors.toList()));
    }

    /**
     * 获取单个用户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Mono<User> getUser(@PathVariable Long id){
        return Mono.justOrEmpty(users.get(id));
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping
    public Mono<ResponseEntity<String>> addUser(User user){
        users.put(user.getId(), user);
        return Mono.just(new ResponseEntity<>("添加成功", HttpStatus.CREATED));
    }

    /**
     * 修改用户
     * @param id
     * @param user
     * @return
     */
    @PutMapping("/{id}")
    public Mono<ResponseEntity<User>> putUser(@PathVariable Long id, User user){
        user.setId(id);
        users.put(id, user);
        return Mono.just(new ResponseEntity<>(user, HttpStatus.CREATED));
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Mono<ResponseEntity<String>> deleteUser(@PathVariable Long id){
        users.remove(id);
        return Mono.just(new ResponseEntity<>("删除成功", HttpStatus.ACCEPTED));
    }

}
